package com.smfx.yxt.controller.teacher;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smfx.yxt.common.AppUser;
import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.MenuConst;
import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.controller.BaseController;
import com.smfx.yxt.model.Grid;
import com.smfx.yxt.model.GridBase;
import com.smfx.yxt.model.SchoolPropertyGoodsStock;
import com.smfx.yxt.model.SchoolPropertyGoodsStockDetail;
import com.smfx.yxt.model.SchoolPropertyGoodsType;
import com.smfx.yxt.model.SchoolPropertyInput;
import com.smfx.yxt.model.SchoolPropertyInputDetail;
import com.smfx.yxt.model.SchoolPropertyOutputRecord;
import com.smfx.yxt.page.OrderBy;
import com.smfx.yxt.page.Page;
import com.smfx.yxt.reqVo.SchoolPropertyGoodStockQueryVo;
import com.smfx.yxt.reqVo.SchoolPropertyGoodTypeQueryVo;
import com.smfx.yxt.reqVo.SchoolPropertyGoodTypeReqVo;
import com.smfx.yxt.reqVo.SchoolPropertyGoodsStockDetailQueryVo;
import com.smfx.yxt.reqVo.SchoolPropertyInputQueryVo;
import com.smfx.yxt.reqVo.SchoolPropertyInputReqVo;
import com.smfx.yxt.reqVo.SchoolPropertyOutputRecordQueryVo;
import com.smfx.yxt.reqVo.SchoolPropertyOutputRecordReqVo;
import com.smfx.yxt.service.ISchoolPropertyService;
import com.smfx.yxt.utils.ValidatorUtil;
import com.smfx.yxt.view.SchoolPropertyGoodsTypeView;
import com.smfx.yxt.view.SchoolPropertyOutputRecordView;
import com.smfx.yxt.view.SchoolPropertyStockView;

/**
 * 校产管理
 * */
@Controller
@RequestMapping(Const.TEACHER_ROLE + MenuConst.SCHOOL_PROERTY)
public class SchoolPropertytroller extends BaseController {

	@Autowired
	private ISchoolPropertyService schoolPropertyService;
	private static final  Logger LOGGER = LoggerFactory.getLogger(SchoolPropertytroller.class);
	
	
	/**
	 * 物品入库
	 * */
	@RequestMapping(value = "inputStock/add",method = RequestMethod.POST)
	@ResponseBody
	public RspResult addStock(SchoolPropertyInputReqVo vo) {
		try {
			//参数校验
			String error = ValidatorUtil.validatorParams(vo);
			if(error != null)
				return new RspResult(Status.FAIL,error);
			//设置学校信息
			AppUser appUser = getAppUser();
			vo.setSchoolId(appUser.getTeacher().getSchoolId());
			vo.setOperatorId(appUser.getTeacher().getId());
			vo.setOperatorName(appUser.getTeacher().getName());
			
			//将入库订单的id返回
			int inputId = schoolPropertyService.insertStock(vo);
			return new RspResult(Status.SUCCESS,inputId);
		} catch (Exception e) {
			LOGGER.error("addStock error",e);
			return new RspResult(Status.FAIL);
		}
	}
	/**
	 * 入库记录查询
	 * */
	@RequestMapping("inputStock/query")
	@ResponseBody
	public RspResult quertyInputRecord(SchoolPropertyInputQueryVo vo) {
		try {
			Page<SchoolPropertyInput> page = getPage(vo);
			List<OrderBy> sorts = new ArrayList<>();
			  sorts.add(new OrderBy("ID","DESC"));
			page.setSorts(sorts);  
			AppUser appUser = getAppUser();
		    vo.setSchoolId(appUser.getTeacher().getSchoolId());
			List<SchoolPropertyInput> list = schoolPropertyService.selectInputRecordByPage(page);
			return new RspResult(Status.SUCCESS,getGrid(page, list));
		} catch (Exception e) {
			LOGGER.error("quertyInputRecord error ",e);
			return new RspResult(Status.FAIL);
		}
	}
	/**
	 * 入库信息详情查询
	 * */
	@RequestMapping("inputStock/detail/query")
	@ResponseBody
	public RspResult queryInputDetail(SchoolPropertyInputQueryVo vo) {
		try {
			Page<SchoolPropertyInputDetail> page = getPage(vo);
			List<OrderBy> sorts = new ArrayList<>();
			  sorts.add(new OrderBy("ID","DESC"));
			page.setSorts(sorts);  
			AppUser appUser = getAppUser();
		    vo.setSchoolId(appUser.getTeacher().getSchoolId());
			List<SchoolPropertyInputDetail> list = schoolPropertyService.selectInputDetailByPage(page);
			return new RspResult(Status.SUCCESS,getGrid(page, list));
		} catch (Exception e) {
			LOGGER.error("queryInputDetail error ",e);
			return new RspResult(Status.FAIL);
		}
	}
	/**
	 * 库存查询
	 * */
	@RequestMapping(value = "stock/query",method = RequestMethod.POST)
	@ResponseBody
	public RspResult stockQuery(SchoolPropertyGoodStockQueryVo reqvo) {
		try {
			Page<SchoolPropertyStockView> page = getPage(reqvo);
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("UPDATE_TIME","DESC"));
			page.setSorts(sorts);  
			AppUser appUser = getAppUser();
			reqvo.setSchoolId(appUser.getTeacher().getSchoolId());
			List<SchoolPropertyStockView> stocks = schoolPropertyService.selectStockByPage(page);
			
			return new RspResult(Status.SUCCESS,getGrid(page, stocks));
		} catch (Exception e) {
			LOGGER.error("stockQuery",e);
			return new RspResult(Status.FAIL);
		}
	}
	/**
	 * 非消耗品的库存详情查询
	 * */
	@RequestMapping("stock/detail/query")
	@ResponseBody
	public RspResult queryStockDetail(SchoolPropertyGoodsStockDetailQueryVo qvo) {
		try {
			Page<SchoolPropertyGoodsStockDetail> page = getPage(qvo);
			List<OrderBy> sorts = new ArrayList<>();
			  sorts.add(new OrderBy("CREATE_TIME","DESC"));
			  page.setSorts(sorts);
			List<SchoolPropertyGoodsStockDetail> list = schoolPropertyService.selectStockDetailByPage(page);
			return new RspResult(Status.SUCCESS,getGrid(page, list));
		} catch (Exception e) {
			LOGGER.error("queryStockDetail error ",e);
			return new RspResult(Status.FAIL);
		}
	}
	/**
	 * 消耗品的库存量查询
	 * */
	@RequestMapping("stock/num/query")
	@ResponseBody
	public RspResult queryStockNum(SchoolPropertyGoodsStockDetailQueryVo qvo) {
		try {
			AppUser appUser = getAppUser();
			qvo.setSchoolId(appUser.getTeacher().getSchoolId());
			SchoolPropertyGoodsStock num = schoolPropertyService.selectStockNumberByParams(qvo);
			return new RspResult(Status.SUCCESS,num);
		} catch (Exception e) {
			LOGGER.error("queryStockNum error ",e);
			return new RspResult(Status.FAIL);
		}
	}
	/**
	 * 分页查询物品的类型
	 * */
	@ResponseBody
	@RequestMapping(value ="type/query",method  = RequestMethod.POST)
	public RspResult typeQuery(SchoolPropertyGoodTypeQueryVo vo) {
		try {
			vo.setSchoolId(1);
			Page<SchoolPropertyGoodsType> page = getPage(vo);
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("CREATE_TIEME","DESC"));
			page.setSorts(sorts);
			vo.setSchoolId(1);
			List<SchoolPropertyGoodsType> result = schoolPropertyService.selectTypeByPage(page);
			return new RspResult(Status.SUCCESS, getGrid(page,result));
		} catch (Exception e) {
			LOGGER.error("typeQuery error ",e);
			return new RspResult(Status.FAIL);
		}
	}
	/**
	 * 查询所有的物品的类型、名称和属性
	 * */
	@ResponseBody
	@RequestMapping(value ="type/all",method  = RequestMethod.POST)
	public RspResult allTypeQuery(SchoolPropertyGoodTypeQueryVo vo) {
		try {
			List<SchoolPropertyGoodsTypeView> result = schoolPropertyService.selectAllBySchoolId(1,vo.getCycleUse());
			return new RspResult(Status.SUCCESS,result);
		} catch (Exception e) {
			LOGGER.error("allTypeQuery error ",e);
			return new RspResult(Status.FAIL);
		}
	}
	/**
	 * 添加物品类型
	 * @param vo 请求参数
	 * */
	@RequestMapping(value ="type/add",method = RequestMethod.POST)
	@ResponseBody
	public RspResult addType(@RequestBody List<SchoolPropertyGoodTypeReqVo> vo) {
		try {
			if(vo == null)
				return new RspResult(Status.FAIL);
			AppUser appUser = getAppUser();
			vo.stream().forEach(e->{
				e.setSchoolId(appUser.getTeacher().getSchoolId());
				schoolPropertyService.insertGoodsType(e);
			});
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			LOGGER.error("addType error ",e);
			return new RspResult(Status.FAIL);
		}
	}
	/**
	 * 物品使用记录查询
	 * */
	@RequestMapping(value ="outputRecord/query",method = RequestMethod.POST)
	@ResponseBody
	public RspResult recordQuery(SchoolPropertyOutputRecordQueryVo vo) {
		    try {
		    	Page<SchoolPropertyOutputRecord> page = getPage(vo);
		    	List<OrderBy> sorts = new ArrayList<>();
				sorts.add(new OrderBy("CREATE_TIME","DESC"));
				page.setSorts(sorts);
				AppUser appUser = getAppUser();
				vo.setSchoolId(appUser.getTeacher().getSchoolId());
		    	List<SchoolPropertyOutputRecord> list = schoolPropertyService.seleOutputRecordByPage(page);
		    	return new RspResult(Status.SUCCESS,getGrid(page, list));
			} catch (Exception e) {
				LOGGER.error("recordQuery error ",e);
				return new RspResult(Status.FAIL);
			}
	}
	/**
	 * 物品使用记录添加
	 * */
	@RequestMapping(value ="outputRecord/add",method = RequestMethod.POST)
	@ResponseBody
	public RspResult recordAdd(SchoolPropertyOutputRecordReqVo vo) {
		try {
			AppUser appUser = getAppUser();
			vo.setOperatorId(appUser.getTeacher().getId());
			vo.setOperttorName(appUser.getTeacher().getName());
			vo.setSchoolId(appUser.getTeacher().getSchoolId());		
			schoolPropertyService.insertOutputRecord(vo);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			LOGGER.error("recordAdd error ",e);
			return new RspResult(Status.FAIL);
		}
	}
	/**
	 * 物品使用记录更新
	 * */
	@RequestMapping(value ="outputRecord/update",method = RequestMethod.POST)
	@ResponseBody
	public RspResult recordUpdate(SchoolPropertyOutputRecordReqVo vo) {
		try {
			AppUser appUser = getAppUser();
            vo.setSchoolId(appUser.getTeacher().getSchoolId());
			schoolPropertyService.updateSchoolPropertyOutputRecord(vo);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			LOGGER.error("recordUpdate error ",e);
			return new RspResult(Status.FAIL);
		}
	}
	
	/**
	 * 校产管理首页
	 * */
	@RequestMapping(value = {"/home","","/"})
	public String index() {
		return "SchoolProperty/schoolPropertyHome";
	}
	
	/**
	 * 库存管理首页
	 * */
	@RequestMapping("/stockIndex")
	public String  stockIndex() {
		return "SchoolProperty/stockIndex";
	}
	/**
	 * 消耗品库存详情
	 * */
	@RequestMapping("/stockIndex/detail")
	public String stockDetail(@RequestParam("id") Integer id,ModelMap mp) {
		try {
			mp.addAttribute("id", id);
		} catch (Exception e) {
			LOGGER.error("stockDetail error",e);
		}
		return"SchoolProperty/stockSDetail";
	}
	/**
	 * 入库管理首页
	 * */
	@RequestMapping("/insertStockIndex")
	public String insertStockIndex(){
		return "SchoolProperty/insertStockIndex";
	}
	
	/**
	 * 入库管理首页
	 * */
	@RequestMapping("/insertStockIndex/detail")
	public String insertStockIndexDetail(){
		return "SchoolProperty/insertStockIndexDetail";
	}
	/**
	 * 入库管理添加
	 * */
	@RequestMapping("/insertStockIndex/add")
	public String insertStockAdd(ModelMap modelMap) {
		return "SchoolProperty/insertStockAdd";
	}
	
	
	/**
	 * 借还管理
	 * */
	@RequestMapping("/lendAndReturnInex")
	public String lendAndReturnIndex() {
		return"SchoolProperty/lendAndReturnInex";
	}
	/**
	 * 借还添加
	 * */
	@RequestMapping("/lendAndReturnInex/add")
	public String lendAndReturnAdd() {
		return"SchoolProperty/lendAndReturnAdd";
	}
	
	/**
	 * 借出详情
	 * */
	@RequestMapping("/lendAndReturnInex/ldetail")
	public String lendIndex(@RequestParam("outputId") Integer outputId,ModelMap mp){
		try {
			SchoolPropertyOutputRecordView record = schoolPropertyService.selectOutputRecordView(outputId);
			mp.addAttribute("item", record);
		} catch (Exception e) {
			LOGGER.error("lendIndex error ",e);
		}
		return "SchoolProperty/lend";
	}
	/**
	 * 归还详情
	 * */
	@RequestMapping("/lendAndReturnInex/rdetail")
	public String retuenIndex(@RequestParam("outputId") Integer outputId,ModelMap mp) {
		try {
			SchoolPropertyOutputRecordView record = schoolPropertyService.selectOutputRecordView(outputId);
			mp.addAttribute("item", record);
		} catch (Exception e) {
			LOGGER.error("retuenIndex error ",e);
		}
		return "SchoolProperty/returnGoods";
	}
	
	
	/**
	 * 消耗品领用首页
	 * */
	@RequestMapping("/consumablesApplyIndex")
	public String consumablesApplyIndex() {
		return "SchoolProperty/consumablesApplyIndex";
	}
	/**
	 * 消耗品领用
	 * */
	@RequestMapping("/consumablesApplyIndex/add")
	public String consumablesApplyAdd() {
		return "SchoolProperty/consumablesApplyAdd";
	}
	
	/**
	 * 报表导出首页
	 * */
	@RequestMapping("/reportExportIndex")
	public String reportExportIndex() {
		return "SchoolProperty/reportExportIndex";
	}
	/**
	 * 物品类型首页
	 * */
	@RequestMapping("goodTypeManageIndex")
	public String goodTypeManageIndex() {
		return "SchoolProperty/goodTypeManageIndex";
	}
	
	@RequestMapping("goodTypeManageIndex/add")
	public String goodTypeManageAdd() {
		return "SchoolProperty/goodTypeManageAdd";
	}
	
	public <T,R> Grid getGrid(Page<T> page,List<T> list) {
		Grid grid = new Grid();
		grid.setTotal(page.getTotalRecord());
		grid.setRows(list);
		return grid;
	}
	
	public<T> Page<T> getPage(GridBase base){
		Page<T> page = new Page<>();
		page.setParams(base);
		page.setPageNo(base.getPage());
		page.setPageSize(base.getRows());
		return page;
	}
}
